<?php
 
    include_once('classes/class.mssql.php');
    
    session_start();
    
    $db = new MSSQL();
    
    $comtec_id_user = $_GET["id"];
    $type = $_GET['t'];
   
    date_default_timezone_set('America/Santiago');
    $date = date("Y").date("m").date("d")." 00:00:00";
    
    if($_SESSION["users"]!="clients"){
     $stmt = $db->query("SELECT DATEADD(day,-15,'$date') as date");
    }else{
          $stmt = $db->query("SELECT DATEADD(day,-30,'$date') as date");
         }
         
    sqlsrv_fetch($stmt);    
    $date2 = sqlsrv_get_field($stmt,0,SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
    $stmt = $db->query("SELECT DATEADD(day,1,'$date') as date");
    sqlsrv_fetch($stmt);
    $date  = sqlsrv_get_field($stmt,0,SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
    $next_day = substr($date2,8,2);
    $next_month = substr($date2,5,2);
    $next_year = substr($date2,0,4);
    $op_day = substr($date,8,2);
    $op_month = substr($date,5,2);
    $op_year = substr($date,0,4);
    
    $date  = $op_day."/".$op_month."/".$op_year; 
    $date2 = $next_day."/".$next_month."/".$next_year;
    
    $tsql  = "SELECT ord.comtec_id_order,o.comtec_id_operation,o.operation_type,m.cod_mainteiner,m.brand,p.company_name,(r.name+' '+r.lastname1+' '+r.lastname2) AS encargado ";
    $tsql .= ",CONVERT(varchar(12),o.operation_date_end,103) as fecha_operacion,m.model,m.comtec_id_machine,s.name_s,ord.cod_order";
    $tsql .= " FROM (((((operation AS o INNER JOIN machines AS m ON o.comtec_id_machine = m.comtec_id_machine) INNER JOIN ";
    $tsql .= " responsible as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN [owner] AS p ";
    $tsql .= " ON m.comtec_id_owner = p.comtec_id_owner) ";
    $tsql .= " INNER JOIN users as u ON m.comtec_id_mainteiner = u.comtec_id_mainteiner) INNER JOIN orders as ord ON 
               ord.comtec_id_order = o.comtec_id_order) INNER JOIN status_op as s ON s.id_s = o.comtec_id_status";

    if($_SESSION["users"]!="clients"){
    $tsql .= " WHERE u.comtec_id_user=$comtec_id_user AND ";
    }else{
    $tsql .= " WHERE p.comtec_id_user=$comtec_id_user AND u.comtec_id_user=$comtec_id_user AND ";    
    }
    
    if ($type == 1){
     $tsql .= " o.comtec_id_status = 2 AND ";   
    }elseif($type == 2){
     $tsql .= " o.comtec_id_status = 3 AND ";     
    }elseif($type == 3){
     $tsql .= " o.operation_date_end <= '$date' AND o.operation_date_end >= '$date2' AND o.comtec_id_status = 4 AND ";    
    }
    
    $tsql .= " o.status_reg=1 
               ORDER BY o.operation_date_end DESC";   
    $stmt = $db->query($tsql);  
    
    $texto = "<table class='table'>
              <tr>";
    
    if($_SESSION["users"] == "managers"){
     $texto .= "<th colspan=2></th>";
    }else{ if($_SESSION["users"] != "clients") { $texto .= "<th></th>";} }        
    
    $texto .= "<th>C&oacute;digo</th>
              <th>Tipo</th>
              <th>C&oacute;d. M&aacute;quina</th>
              <th>Marca</th>
              <th>Modelo</th>            
              <th>Propietario</th>
              <th>Encargado</th>";
    $texto .= "<th>Fecha oper.</th>";
    if(strcmp($_SESSION["users"],"clients")!=0){
     $texto .= "<th>Cód. Orden</th>
                <th>Estado</th>";
    }
    $texto .= "</tr>";  
    $cont = 0;
    while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){ 
     $cont += 1;   
     $comtec_id_operation=$row['comtec_id_operation'];  
     $comtec_id_machine=$row['comtec_id_machine'];
     $operation_type=$row['operation_type'];     
     $cod_mainteiner=$row['cod_mainteiner'];     
     $brand=$row['brand'];
     $model=$row['model'];
     $company_name=$row['company_name'];
     $encargado=$row['encargado'];
     $fecha_operacion=$row['fecha_operacion'];
     $status=$row['name_s'];
     $cod_order=$row['cod_order'];
     $comtec_id_order=$row['comtec_id_order'];
     $texto .= "<tr>";
     if($_SESSION["users"]!="clients"){
      $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
      $texto .= "<td><a href='javascript:loadOperation(".$comtec_id_operation.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";
     }
     if($_SESSION["users"]=="managers"){
      $url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
      $texto .= "<td><a href='javascript:deleteOperation(".$comtec_id_operation.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";
     }
     $texto .= "<td><a href='javascript:operation(".$comtec_id_operation.");'>$comtec_id_operation</a></td>";
     switch ($operation_type) {
         case 1:
                $operation_type="Mantenci&oacute;n";
         break;
         case 2:
                $operation_type="Emergencia";
         break;
         case 3:
                $operation_type="Instalaci&oacute;n";
         break;
         case 4:
                $operation_type="Reparaci&oacute;n";
         break;    
     }
     $texto .= "<td>$operation_type</td>";
     $texto .= "<td>$cod_mainteiner</td>";
     $texto .= "<td>$brand</td>";
     $texto .= "<td>$model</td>";
     $texto .= "<td>$company_name</td>";
     $texto .= "<td>$encargado</td>";
     $texto .= "<td>$fecha_operacion</td>";
     if($_SESSION["users"]!="clients"){
      $cod_order   = ($cod_order != "")? $cod_order : "-";
      $texto .= "<td><a href='javascript:order(".$comtec_id_order.");' title='Ver orden de trabajo'>$cod_order</a></td>";
      $texto .= "<td>$status</td>";
     }
     $texto .= "</tr>";
    }
     $texto .= "</table>";
     echo "<p class='total'>Total: $cont</p>".$texto."<div id='result'></div>";
?>
